package com.amos.generator.model.impl;

import com.amos.generator.connect.Connector;
import com.amos.generator.model.BaseGeneratorImpl;
import com.amos.generator.starter.GeneratorContext;
import com.amos.generator.starter.PackageConfigType;
import com.amos.generator.starter.PackageConfigTypes;
import com.amos.generator.util.GeneratorStringUtils;
import com.amos.generator.util.PropertiesUtils;
import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 功能描述：Model代码生成
 */
@Component
public class ControllerGeneratorImpl extends BaseGeneratorImpl {

    @Override
    public void initVelocityContext(VelocityContext velocityContext, GeneratorContext generatorContext) {
        super.initVelocityContext(velocityContext, generatorContext);
        velocityContext.put("author", PropertiesUtils.getString("generator.authorName"));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        velocityContext.put("date", dateFormat.format(new Date()));
        Connector connector = (Connector) generatorContext.getAttribute("connector");
        String tableName = generatorContext.getTableName();
        Map<String, String> keyMap = connector.getPrimaryKey(tableName);
        Map<String, String> colMap = connector.mapColumnNameType(tableName);
        Map<String, String> columnRemarkMap = connector.mapColumnRemark(tableName);
        Map<String, String> isNull = connector.mapNull(tableName);
        List<String> inserts = generateInsert(colMap, columnRemarkMap, keyMap, isNull);
        List<String> updates = generateUpdate(colMap, columnRemarkMap, keyMap, isNull);
        velocityContext.put("inserts", inserts);
        velocityContext.put("updates", updates);
    }

    @Override
    public PackageConfigTypes getPackageConfigTypes() {
        if (super.getPackageConfigTypes() == null || super.getPackageConfigTypes().getType() == null) {
            Set<PackageConfigType> packageConfigTypeSet = new HashSet();
            packageConfigTypeSet.add(new PackageConfigType("controllerPackage", "/controller", "Controller.java", "controller.vm"));
            PackageConfigTypes packageConfigTypes = new PackageConfigTypes();
            packageConfigTypes.setPackageConfigTypeSet(packageConfigTypeSet);
            packageConfigTypes.setType(PackageConfigTypes.ConfigType.CONTROLLER);
            setPackageConfigTypes(packageConfigTypes);
        }
        return super.getPackageConfigTypes();
    }

    protected List<String> generateInsert(Map<String, String> map, Map<String, String> columnRemarkMap, Map<String, String> keyMap, Map<String, String> isNull) {
        Set<String> keySet = map.keySet();
        List<String> fields = new ArrayList();
        for (String key : keySet) {
            if (keyMap.get("primaryKey").equals(key))
                continue;
            if (key.equals("create_time") || key.equals("update_time") || key.equals("create_user") || key.equals("update_user"))
                continue;

            StringBuilder sb = new StringBuilder();
            sb.append(" @ApiImplicitParam(name = \"").append(GeneratorStringUtils.format(key)).append("\", value = \"")
                    .append(columnRemarkMap.get(key).trim())
                    .append("\", paramType = \"form\", ");
            if (isNull.get(key).equals("NO")) {
                sb.append("required = true, ");
            } else {
                sb.append("required = false, ");
            }

            if (map.get(key).toLowerCase().equals("integer")) {
                sb.append(" dataType = \"int\"),");
            } else if (map.get(key).toLowerCase().equals("bigdecimal")) {
                sb.append(" dataType = \"double\"),");
            }  else if (map.get(key).toLowerCase().equals("long")) {
                sb.append(" dataType = \"long\"),");
            } else {
                sb.append(" dataType = \"string\"),");
            }
            fields.add(sb.toString());
        }
        //String token = " @ApiImplicitParam(name = \"ticket\", value = \"用户token\", paramType = \"header\", required = true, dataType = \"string\")";
        //fields.add(token);
        return fields;
    }

    protected List<String> generateUpdate(Map<String, String> map, Map<String, String> columnRemarkMap, Map<String, String> keyMap, Map<String, String> isNull) {
        Set<String> keySet = map.keySet();
        List<String> fields = new ArrayList();

        String id = " @ApiImplicitParam(name = \"id\", value = \"主键id\", paramType = \"path\", required = true, dataType = \"long\"),";
        fields.add(id);
        for (String key : keySet) {
            if (keyMap.get("primaryKey").equals(key))
                continue;
            if (key.equals("create_time") || key.equals("update_time") || key.equals("create_user") || key.equals("update_user"))
                continue;

            StringBuilder sb = new StringBuilder();
            sb.append(" @ApiImplicitParam(name = \"").append(GeneratorStringUtils.format(key)).append("\", value = \"")
                    .append(columnRemarkMap.get(key).trim())
                    .append("\", paramType = \"form\", ");

            sb.append("required = false, ");

            if (map.get(key).toLowerCase().equals("integer")) {
                sb.append(" dataType = \"int\"),");
            } else if (map.get(key).toLowerCase().equals("bigdecimal")) {
                sb.append(" dataType = \"double\"),");
            } else if (map.get(key).toLowerCase().equals("long")) {
                sb.append(" dataType = \"long\"),");
            } else {
                sb.append(" dataType = \"string\"),");
            }
            fields.add(sb.toString());
        }
        //String token = " @ApiImplicitParam(name = \"ticket\", value = \"用户token\", paramType = \"header\", required = true, dataType = \"string\"),";
        //fields.add(token);
        return fields;
    }
}
